package com.baichang.taoshunda.utils;

import net.sf.json.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.List;

public class ParserTool {

    // /////////////////////////////http 请求/////////////////////////////////////

    /**
     * post 获取 rest 资源
     *
     * @param url
     * @param name_value_pair
     * @return
     * @throws IOException
     */
    public static String do_post(String url, List<NameValuePair> name_value_pair) throws IOException {
        String body = "{}";
        DefaultHttpClient httpclient = new DefaultHttpClient();
        try {
            HttpPost httpost = new HttpPost(url);
            httpost.setEntity(new UrlEncodedFormEntity(name_value_pair, StandardCharsets.UTF_8));
            HttpResponse response = httpclient.execute(httpost);
            HttpEntity entity = response.getEntity();
            body = EntityUtils.toString(entity);
        } finally {
            httpclient.getConnectionManager().shutdown();
        }
        return body;
    }

    /**
     * get 获取 rest 资源
     *
     * @param url
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     */
    public static String do_get(String url) throws ClientProtocolException, IOException {
        String body = "{}";
        DefaultHttpClient httpclient = new DefaultHttpClient();
        try {
            HttpGet httpget = new HttpGet(url);
            HttpResponse response = httpclient.execute(httpget);
            HttpEntity entity = response.getEntity();
            body = EntityUtils.toString(entity);
        } finally {
            httpclient.getConnectionManager().shutdown();
        }
        return body;
    }

    public static String getAdd(String log, String lat) {
        String ak = "0uvsSrdr5oaZ68dVFyUBCUsK";
        //参数解释: 纬度,经度 type 001 (100代表道路，010代表POI，001代表门址，111可以同时显示前三项)
        String urlString = "http://api.map.baidu.com/geocoder/v2/?location=" + lat + "," + log + "&output=json&pois=1&ak=" + ak;
        String district = null;
        try {
            JSONObject jsonObject = JSONObject.fromObject(ParserTool.do_get(urlString));
            JSONObject result = JSONObject.fromObject(jsonObject.get("result"));
            JSONObject addressComponent = JSONObject.fromObject(result.get("addressComponent"));
            district = addressComponent.get("district").toString();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return district;

    }

    public static void main(String[] args) throws IOException {
        String ak = "0uvsSrdr5oaZ68dVFyUBCUsK";

        String url = "http://api.map.baidu.com/geocoder/v2/?location=" + "37.429581,116.377533&output=json&pois=1&ak=" + ak;
//        String url = "http://api.map.baidu.com/api?v=2.0&ak=" + ak;
//       http://api.map.baidu.com/geocoder/v2/?callback=renderReverse&location=39.934,116.329&output=json&pois=1&ak=您的ak //GET请求
        //http://api.map.baidu.com/api?v=2.0&ak=您的密钥
        //http://api.map.baidu.com/geocoder/v2/?ak=pmCgmADsAsD9rEXkqWNcTzjd&location=30.548397,104.04701&output=json&pois=1
        JSONObject jsonObject = JSONObject.fromObject(ParserTool.do_get(url));
        JSONObject result = JSONObject.fromObject(jsonObject.get("result"));
        JSONObject addressComponent = JSONObject.fromObject(result.get("addressComponent"));
        String district = addressComponent.get("district").toString();
        System.out.println(district);
        System.out.print(do_get(url));
    }
}